package com.mf.ergate.web.controller.auth;

import com.mf.ergate.api.auth.model.dto.*;
import com.mf.ergate.api.auth.model.vo.ResourceInfoVo;
import com.mf.ergate.api.auth.model.vo.RoleInfoVo;
import com.mf.ergate.api.auth.model.vo.RoleUserRelationVo;
import com.mf.ergate.web.common.exception.BusinessException;
import com.mf.ergate.web.common.utils.MfParamUtils;
import com.mf.ergate.web.domain.enums.auth.RoleTypeEnum;
import com.mf.ergate.web.service.auth.RoleInfoService;
import com.mf.framework.common.model.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.*;

/**
 * 角色信息Controller
 * User: zhaoming
 * DateTime: 2018-03-02
 * To change this template use File | Settings | File Templates.
 */
@Secured({"MANAGE_ROLE_INFO"})
@Controller
@RequestMapping("/web/roleInfo")
public class RoleInfoController{

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private RoleInfoService roleInfoService;

    /**
     * 首页
     * @return 模型视图
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("auth/role/role_info_index");
        modelAndView.addObject("RoleTypeEnum", RoleTypeEnum.getList());
        return modelAndView;
    }

    /**
     * 新增页面
     * @return 模型视图
     */
    @RequestMapping(value = "/addPage", method = RequestMethod.GET)
    public ModelAndView addPage() {
        ModelAndView modelAndView = new ModelAndView("auth/role/role_info_add");
        modelAndView.addObject("RoleTypeEnum", RoleTypeEnum.getList());
        return modelAndView;
    }

    /**
     * 维护页面
     * @param uniqueNo 角色编号
     * @return 模型视图
     */
    @RequestMapping(value = "/editPage", method = RequestMethod.GET)
    public ModelAndView editPage(String uniqueNo) {
        ModelAndView modelAndView = new ModelAndView("auth/role/role_info_edit");
        modelAndView.addObject("uniqueNo", uniqueNo);
        modelAndView.addObject("RoleTypeEnum", RoleTypeEnum.getList());
        return modelAndView;
    }

    /**
     * 资源配置页面
     * @param uniqueNo 角色编号
     * @return 模型视图
     */
    @RequestMapping(value = "/resourceConfigPage", method = RequestMethod.GET)
    public ModelAndView resourceConfigPage(String uniqueNo) {
        ModelAndView modelAndView = new ModelAndView("auth/role/role_info_config_resource");
        modelAndView.addObject("roleInfo", roleInfoService.selectByUniqueNo(uniqueNo));
        return modelAndView;
    }

    /**
     * 用户配置页面
     * @param uniqueNo 角色编号
     * @return 模型视图
     */
    @RequestMapping(value = "/userConfigPage", method = RequestMethod.GET)
    public ModelAndView userConfigPage(String uniqueNo) {
        ModelAndView modelAndView = new ModelAndView("auth/role/role_info_config_user");
        modelAndView.addObject("roleInfo", roleInfoService.selectByUniqueNo(uniqueNo));
        return modelAndView;
    }

    /**
     * 新增角色信息
     * @param roleInfoCreateDto 角色信息新增Dto
     * @return 执行结果
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public RoleInfoVo insert(RoleInfoCreateDto roleInfoCreateDto) {
        MfParamUtils.buildInsertEntity(roleInfoCreateDto);
        return roleInfoService.insert(roleInfoCreateDto);
    }

    /**
     * 修改
     * @param roleInfoUpdateDto 角色信息更新Dto
     * @return 执行结果
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean update(RoleInfoUpdateDto roleInfoUpdateDto) {
        MfParamUtils.buildUpdateEntity(roleInfoUpdateDto);
        return roleInfoService.updateByUniqueNo(roleInfoUpdateDto);
    }

    /**
     * 根据主键删除角色相关信息
     * @param roleInfoDeleteDto 角色信息删除Dto
     * @return 执行结果
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean delete(RoleInfoDeleteDto roleInfoDeleteDto) {
        MfParamUtils.buildUpdateEntity(roleInfoDeleteDto);
        return roleInfoService.deleteCompleteByUniqueNo(roleInfoDeleteDto);
    }

    /**
     * 根据主键查询
     * @param roleInfoQueryDto 角色信息查询Dto
     * @return 角色信息
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public RoleInfoVo detail(RoleInfoQueryDto roleInfoQueryDto) {
        return roleInfoService.selectByUniqueNo(roleInfoQueryDto.getUniqueNo());
    }

    /**
     * 根据条件查询
     * @param roleInfoQueryDto 角色信息查询Dto
     * @return 角色信息列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public List<RoleInfoVo> list(RoleInfoQueryDto roleInfoQueryDto) {
        return roleInfoService.selectByCondition(roleInfoQueryDto);
    }

    /**
     * 根据条件分页查询
     * @param roleInfoQueryDto 角色信息查询Dto
     * @param pageNum 当前页码
     * @param pageSize 每页显示条数
     * @param sortName 排序字段
     * @param sortOrder 排序方式
     * @return 角色信息分页列表
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Page<RoleInfoVo> page(RoleInfoQueryDto roleInfoQueryDto,
                                 @RequestParam(value = "page") Integer pageNum,
                                 @RequestParam(value = "rows") Integer pageSize,
                                 @RequestParam(value = "sidx") String sortName,
                                 @RequestParam(value = "sord") String sortOrder) {
        Page page = MfParamUtils.buildPage(pageNum, pageSize, sortName, sortOrder);
        return roleInfoService.selectPage(page, roleInfoQueryDto);
    }

    /**
     * 检查重复数据
     * @return 验证结果
     */
    @RequestMapping(value = "/checkRepeat", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> checkRepeat(RoleInfoQueryDto roleInfoQueryDto) {
        Map<String, Object> map = new HashMap<>();
        if(roleInfoService.selectCount(roleInfoQueryDto) > 0){
            map.put("error", "角色编码已被占用");
        }
        return map;
    }

    /**
     * 角色与用户关联分页查询
     * @param roleUserRelationQueryDto 用户与角色关联查询Dto
     * @param pageNum 当前页码
     * @param pageSize 每页显示条数
     * @param sortName 排序字段
     * @param sortOrder 排序方式
     * @return 用户角色关联信息分页列表
     */
    @RequestMapping(value = "/roleUserRelationPage", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Page<RoleUserRelationVo> roleUserRelationPage(RoleUserRelationQueryDto roleUserRelationQueryDto,
                                                         @RequestParam(value = "page") int pageNum,
                                                         @RequestParam(value = "rows") int pageSize,
                                                         @RequestParam(value = "sidx") String sortName,
                                                         @RequestParam(value = "sord") String sortOrder) {

        Page page = MfParamUtils.buildPage(pageNum, pageSize, sortName, sortOrder);
        return roleInfoService.roleUserRelationPage(page, roleUserRelationQueryDto);
    }

    /**
     * 根据条件查询资源列表
     *
     * @param roleResourceRelationQueryDto 角色与资源关联查询Dto
     * @return 角色与菜单关联关系列表
     */
    @RequestMapping(value = "/listResource", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public List<ResourceInfoVo> listResource(RoleResourceRelationQueryDto roleResourceRelationQueryDto) {
        return roleInfoService.listResource(roleResourceRelationQueryDto);
    }

    /**
     * 保存角色与资源关联信息
     *
     * @param roleNo 角色编号
     * @param resourceNos   资源编号数组
     * @return 执行结果
     */
    @RequestMapping(value = "/saveRoleResourceRelation", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Boolean saveRoleResourceRelation(String roleNo, @RequestParam(value = "resourceNos[]", required = false) String[] resourceNos) {
        List<RoleResourceRelationCreateDto> roleResourceRelationCreateDtos = new ArrayList<>();
        if (Objects.isNull(resourceNos)){
            throw new BusinessException("资源不能为空");
        }
        for (String resourceNo : resourceNos) {
            RoleResourceRelationCreateDto roleResourceRelationCreateDto = new RoleResourceRelationCreateDto();
            roleResourceRelationCreateDto.setRoleNo(roleNo);
            roleResourceRelationCreateDto.setResourceNo(resourceNo);
            MfParamUtils.buildInsertEntity(roleResourceRelationCreateDto);
            roleResourceRelationCreateDtos.add(roleResourceRelationCreateDto);
        }

        return roleInfoService.saveRoleResourceRelation(roleResourceRelationCreateDtos);
    }

}